package com.pig4cloud.pig.module.controller.gate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.gate.BarrierGatePayRole;
import com.pig4cloud.pig.module.service.gate.IBarrierGatePayRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 道闸收费规则Controller
 *
 * @author ruoyi
 * @date 2023-06-23
 */
@Validated
@Api(value = "道闸收费规则控制器", tags = {"道闸收费规则管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/barrier/gate/role")
public class BarrierGatePayRoleController {

    private final IBarrierGatePayRoleService barrierGatePayRoleService;

    /**
     * 查询道闸收费规则列表
     */
    @ApiOperation("查询道闸收费规则列表")
    @PreAuthorize("@pms.hasPermission('module:barrierRole:list')")
    @GetMapping("/list")
    public TableDataInfo<BarrierGatePayRole> list(@Validated BarrierGatePayRole barrierGatePayRole) {
        QueryWrapper<BarrierGatePayRole> qw = new QueryWrapper<>();

        TableDataInfo<BarrierGatePayRole> td = PageUtils.buildDataInfo(barrierGatePayRoleService.page(PageUtils.buildPage(),qw));
        List<BarrierGatePayRole> list = td.getData();
        return td;
    }

    /**
     * 获取道闸收费规则详细信息
     */
    @ApiOperation("获取道闸收费规则详细信息")
    @PreAuthorize("@pms.hasPermission('module:barrierRole:query')")
    @GetMapping("/{id}")
    public R<BarrierGatePayRole> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        BarrierGatePayRole barrierGatePayRole = barrierGatePayRoleService.getById(id);
        return R.ok(barrierGatePayRole);
    }

    /**
     * 新增道闸收费规则
     */
    @ApiOperation("新增道闸收费规则")
    @PreAuthorize("@pms.hasPermission('module:barrierRole:add')")
    @SysLog("道闸收费规则")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody BarrierGatePayRole barrierGatePayRole) {
        return R.okByRow(barrierGatePayRoleService.save(barrierGatePayRole) ? 1 : 0);
    }

    /**
     * 修改道闸收费规则
     */
    @ApiOperation("修改道闸收费规则")
    @PreAuthorize("@pms.hasPermission('module:barrierRole:edit')")
    @SysLog("道闸收费规则")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody BarrierGatePayRole barrierGatePayRole) {
        return R.okByRow(barrierGatePayRoleService.updateById(barrierGatePayRole) ? 1 : 0);
    }

    /**
     * 删除道闸收费规则
     */
    @ApiOperation("删除道闸收费规则")
    @PreAuthorize("@pms.hasPermission('module:barrierRole:remove')")
    @SysLog("道闸收费规则")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(barrierGatePayRoleService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
